package bishi.Huawei;


import java.util.*;

/**
 * @Classname Main2
 * @Description TODO
 * @Date 2021/9/1 19:57
 * @Created by ASUS
 */
public class Main2 {

    static class Node {
        String name;
        List<Node> instances;
        List<Node> subClasses;
        Node(String name, List<Node> instances, List<Node> subClasses) {
            this.name = name;
            this.instances = instances;
            this.subClasses = subClasses;
        }
    }

    static Map<String, Node> map = new HashMap<>();
    static List<String> res = new ArrayList<>();

    /***
     * 3
     * student subClassOf person
     * Tom instanceOf student
     * Marry instanceOf person
     * person
     */
    public static void solution() {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            String[] s = scanner.nextLine().split(" ");
            Node a = map.getOrDefault(s[0], new Node(s[0], new ArrayList<>(), new ArrayList<>()));
            Node b = map.getOrDefault(s[2], new Node(s[2], new ArrayList<>(), new ArrayList<>()));
            if (s[1].equals("subClassOf")) { // a是b的子概念
                b.subClasses.add(a);
            } else if (s[1].equals("instanceOf")) {// a是b的实例
                b.instances.add(a);
            }
            map.put(s[0], a);
            map.put(s[2], b);
        }
        String target = scanner.nextLine();
        scanner.close();
        Node root = map.get(target);
        dfs(root);
    }

    public static void dfs(Node root) {
        List<Node> instances = root.instances;
        List<Node> subClasses = root.subClasses;
        if (instances.size() != 0) {
            for (Node instance : instances) {
                res.add(instance.name);
            }
        }
        if (subClasses.size() != 0) {
            for (Node subClass : subClasses) {
                dfs(subClass);
            }
        }
    }

    public static void main(String[] args) {
        solution();
        Collections.sort(res);
        String join = String.join(" ", res);
        System.out.println(join);
    }
}
